package org.ala.tiktools.rpc.kcp.config;

import org.springframework.core.env.Environment;

import io.netty.util.NettyRuntime;

/**
 * @author ala
 * @date 2025-04-11 11:45
 */
public class RpcKcpConfigInfoParser {
    /**
     *
     */
    public static final String PREFIX  = "ala-rpc.kcp";


    public RpcKcpConfigInfo parseKcp(Environment env) {
        RpcKcpConfigInfo c = new RpcKcpConfigInfo();

        String ports = env.getProperty(PREFIX + ".ports", String.class, "51001");
        String[] ss = ports.split(",");
        int[] ps = new int[ss.length];
        for (int i = 0 ; i < ss.length ; i++) {
        	ps[i] = Integer.parseInt(ss[i].trim());
        }
        c.setPorts(ps);
        c.setWorkerCount(env.getProperty(PREFIX + ".worker_count", Integer.class, -1));
        if (c.getWorkerCount() <= 0) {c.setWorkerCount(NettyRuntime.availableProcessors() * 2);}

        c.setUdpRecvBufferAllocateSize(env.getProperty(PREFIX + ".udp_recv_buffer_allocate_size", Integer.class, 2048));
        c.setCloseWaitTime(env.getProperty(PREFIX + ".close_wait_time", Integer.class, 5000));
        c.setUdpSendBufferSize(env.getProperty(PREFIX + ".udp_send_buffer_size", Integer.class, 50));
        c.setUdpSendFlushProb(env.getProperty(PREFIX + ".udp_send_flush_prob", Double.class, 0.5d));
        
        c.setiKcpRtoNdl(env.getProperty(PREFIX + ".ikcp_rto_ndl", Integer.class, 30));
        c.setiKcpRtoMin(env.getProperty(PREFIX + ".ikcp_rto_min", Integer.class, 100));
        c.setiKcpRtoDef(env.getProperty(PREFIX + ".ikcp_rto_def", Integer.class, 200));
        c.setiKcpRtoMax(env.getProperty(PREFIX + ".ikcp_rto_max", Integer.class, 60000));

        c.setiKcpWndSnd(env.getProperty(PREFIX + ".ikcp_wnd_snd", Integer.class, 32));
        c.setiKcpWndRcv(env.getProperty(PREFIX + ".ikcp_wnd_rcv", Integer.class, 128));

        c.setiKcpMtuDef(env.getProperty(PREFIX + ".ikcp_mtu_def", Integer.class, 1400));
        c.setiKcpAckFast(env.getProperty(PREFIX + ".ikcp_ack_fast", Integer.class, 3));
        c.setiKcpInterval(env.getProperty(PREFIX + ".ikcp_inteval", Integer.class, 100));
        c.setiKcpDeadLink(env.getProperty(PREFIX + ".ikcp_dead_link", Integer.class, 20));
        c.setiKcpThreshInit(env.getProperty(PREFIX + ".ikcp_thresh_init", Integer.class, 2));
        c.setiKcpThreshMin(env.getProperty(PREFIX + ".ikcp_thresh_min", Integer.class, 2));
        c.setiKcpProbeInit(env.getProperty(PREFIX + ".ikcp_probe_init", Integer.class, 7000));
        c.setiKcpProbeLimit(env.getProperty(PREFIX + ".ikcp_probe_limit", Integer.class, 120000));
        c.setiKcpNoDelay(env.getProperty(PREFIX + ".ikcp_no_delay", Boolean.class, false));
        c.setiKcpFastAckLimit(env.getProperty(PREFIX + ".ikcp_fask_ack_limit", Integer.class, 3));
        c.setIkcpFastFlush(env.getProperty(PREFIX + ".ikcp_fast_flush", Boolean.class, false));
        c.setIkcpMergeSegment(env.getProperty(PREFIX + ".ikcp_merge_segment", Boolean.class, true));
        return c;
    }
}
